package com.yiren.dbaa.module.practice_set.line.presenter;


import android.app.Activity;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.line.contract.LineHomeContract;
import com.yiren.dbaa.module.practice_set.line.model.LineHomeModel;
import com.yiren.dbaa.module.practice_set.site.bean.LineDataValBean;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/15 14:42
 * @description: 线路首页
 */
public class LineHomePresenter implements LineHomeContract.Presenter {

    private Activity activity;
    private LineHomeContract.Model lineHomeModel;
    private UserParamConfigBean userParamConfig;

    public LineHomePresenter(Activity activity) {
        this.activity = activity;
        lineHomeModel = new LineHomeModel(activity);
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void loadLineList(LineHomeContract.View view, String siteCode) {
        //刷新缓存
        //从服务器获取线路列表 合伙人获取自己已发布线路，教练获取所属合伙人已发布线路
        LoadDialog.show(activity, "正在加载...");
        lineHomeModel.getLineFromServer(userParamConfig.getUserInfo().getpUserId().toString(), siteCode,false, new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<LineInfoEntity> serverLines = (List<LineInfoEntity>) result;
                //获取本地列表
                List<LineInfoEntity> localLines = GradeDbController.getInstance(false).queryAll(LineInfoEntity.class);
                if (null == serverLines) {
                    view.showLineList(localLines);
                    return;
                }
                if (null == localLines) {
                    view.showLineList(serverLines);
                    return;
                }
                //数据合并去重，服务器与本地数据都存在以本地数据为准
                view.showLineList(lineHomeModel.lineHomedataMerge(localLines, serverLines));
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
                //获取失败加载本地
                List<LineInfoEntity> localLines = GradeDbController.getInstance(false).queryAll(LineInfoEntity.class);
                for (LineInfoEntity lineInfo : localLines) {
                    lineInfo.setDownLoad(true);
                }
                view.showLineList(localLines);
            }
        });
    }

    @Override
    public void downLoadLineData(LineInfoEntity lineInfo, LineHomeContract.View view) {
        int pUserID;
        int cUserID;
        if (userParamConfig.isPartner()) {
            pUserID = userParamConfig.getUserInfo().getId();
            cUserID = userParamConfig.getUserInfo().getId();
        } else {
//            pUserID = userParamConfig.getUserInfo().getpUserId();
            pUserID = userParamConfig.getUserInfo().getId();
            cUserID = userParamConfig.getUserInfo().getId();
        }
        lineHomeModel.downLoadLinePointData(lineInfo.getName(), lineInfo.getLineVer(), userParamConfig.getSite().getSiteCode(), pUserID, cUserID, new CommonCallBack() {
            @Override
            public void success(Object result) {
                //本地数据插入 
                LineDataValBean lineDataValBean = (LineDataValBean) result;
                lineInfo.setPartneModifyVal(lineDataValBean.getPartnerLineData());
                lineInfo.setCoachModifyVal(lineDataValBean.getCoachLineData());

                if (GradeDbController.getInstance(false).insertOrReplace(lineInfo)) {
                    //通知视图
                    LoadDialog.dismiss(activity);
                    MessageBox.MessageAndVoiceModel(activity, "下载线路成功");
                    view.refreshLineList();
                } else {
                    MessageBox.MessageAndVoiceModel(activity, "下载线路失败");
                }
            }

            @Override
            public void fail(String error) {
                super.fail(error);
            }
        });
    }

    @Override
    public void deleteLine(LineInfoEntity lineInfo, CommonCallBack callBack) {
        if (GradeDbController.getInstance(false).delete(lineInfo)) {
            callBack.success("");
        }
    }
}
